import 'package:sqflite_common_ffi/sqflite_ffi.dart';

class DB {
  late Database _database;

  Database get database => _database;

  DB._internal();

  static DB? _db;

  static DB get instance {
    _db ??= DB._internal();
    return _db!;
  }

  Future<void> init() async {
    // var dbpath = await getDatabasesPath();
    // print('dbpath = $dbpath');
    _database = await openDatabase(
      r'C:\home\site_hub.db',
      version: 1,
    );
    initDefaultData();
  }

  Future<void> initDefaultData() async {
    List<Map<dynamic, dynamic>> s = await database.rawQuery('''
    SELECT name FROM sqlite_master WHERE type='table' AND name='site_collect'
    ''');
    if (s.isNotEmpty) {
      return;
    }
    await database.execute('''
    create table if not exists site_collect
    (
    id        integer not null
        constraint table_name_pk
            primary key autoincrement,
    site_name text    not null,
    icon      text    not null,
    site_url  text    not null
    )
    ''');
    await database.execute('''
    insert into site_collect (site_name, icon, site_url)
    values
    ('oschina', 'https://static.oschina.net/new-osc/img/favicon.ico',
    'https://www.oschina.net/'),
    ('mdn', 'https://developer.mozilla.org/favicon-192x192.png',
    'https://developer.mozilla.org/'),
    ('github', 'https://github.githubassets.com/favicons/favicon.png',
    'https://github.com'),
    ('pub.dev', 'https://pub.dev/favicon.ico?hash=nk4nss8c7444fg0chird9erqef2vkhb8',
    'https://pub.dev/'),
    ('juejin', 'https://lf-web-assets.juejin.cn/obj/juejin-web/xitu_juejin_web/static/favicons/favicon-32x32.png',
    'https://juejin.cn/'),
    ('zhihu', 'https://static.zhihu.com/heifetz/favicon.ico',
    'https://www.zhihu.com/'),
    ('feng', 'https://www.feng.com/favicon.ico',
    'https://www.feng.com/'),
    ('gitee', 'https://gitee.com/favicon.ico',
    'https://gitee.com/'),
    ('36kr', 'https://www.36kr.com/favicon.ico',
    'https://www.36kr.com/'),
    ('sef', 'https://static.segmentfault.com/main_site_next/bf618e01/favicon.ico',
    'https://segmentfault.com/'),
    ('arco', 'https://unpkg.byted-static.com/latest/byted/arco-config/assets/favicon.ico',
    'https://arco.design/'),
    ('antd', 'https://aliyuncdn.antdv.com/favicon.ico',
    'https://www.antdv.com/docs/vue/introduce-cn/'),
    ('hellog', 'https://hellogithub.com/favicon/favicon-32x32.png',
    'https://hellogithub.com/'),
    ('fchina', 'http://www.flutterchina.net.cn/img/favicon.png',
    'http://www.flutterchina.net.cn/'),
    ('today', 'https://tophub.today/favicon.ico',
    'https://tophub.today/'),
    ('dev.to', 'https://res.cloudinary.com/practicaldev/image/fetch/s--E8ak4Hr1--/c_limit,f_auto,fl_progressive,q_auto,w_32/https://dev-to.s3.us-east-2.amazonaws.com/favicon.ico',
    'https://dev.to/'),
    ('scottp', 'https://scottplot.net/favicon.ico',
    'https://scottplot.net/'),
    ('1o24', 'https://1024bbs.s3.dualstack.us-east-2.amazonaws.com/optimized/2X/2/234ff411a255503727a8a5157fa9cb15c66204a4_2_512x512.png',
    'https://1o24bbs.com/'),
    ('shader', 'https://www.shadertoy.com/img/favicon.ico',
    'https://www.shadertoy.com/'),
    ('m3d', 'https://m3.material.io/static/assets/m3-favicon.ico',
    'https://m3.material.io/'),
    ('bilibili', 'https://i0.hdslb.com/bfs/static/jinkela/long/images/favicon.ico',
    'https://www.bilibili.com/'),
    ('bing', 'https://cn.bing.com/favicon.ico',
    'https://cn.bing.com/'),
    ('unsplash', 'https://unsplash-assets.imgix.net/favicon/favicon-32x32.png',
    'https://unsplash.com/'),
    ('lapa', 'https://www.lapa.ninja/favicon.ico',
    'https://www.lapa.ninja/'),
    ('huaban', 'https://huaban.com/favicon.ico',
    'https://huaban.com/'),
    ('figma', 'https://static.figma.com/app/icon/1/favicon.png',
    'https://www.figma.com/'),
    ('material', 'https://materialuicolors.co/img/favicon.ico',
    'https://materialuicolors.co/'),
    ('skycity', 'https://spcn-webfront.skypixel.com/skypixel/public/favicon.ico',
    'https://www.skypixel.com/'),
    ('zhan', 'https://static.zcool.cn/git_z/z/site/favicon.ico?version=1618914637608',
    'https://www.zcool.com.cn/'),
    ('shanbay', 'https://assets0.baydn.com/static/img/shanbay_favicon.png',
    'https://web.shanbay.com/web/main'),
    ('fofa', 'https://fofa.info/favicon.ico',
    'https://fofa.info/result?qbase64=dGl0bGU9PSJDaGF0R1BUIFdlYiI%3D'),
    ('paradi', 'https://online.visual-paradigm.com/favicon-32x32.png',
    'https://online.visual-paradigm.com/cn/'),
    ('cowtra', 'https://background.c-t.work/favicon-32x32.png',
    'https://cowtransfer.com/'),
    ('bizhi', 'https://bz.zzzmh.cn/img/icons/favicon-32x32.png',
    'https://bz.zzzmh.cn/index'),
    ('aida', 'https://adzhp.xyz/wp-content/uploads/2019/02/A.png',
    'https://adzhp.xyz/'),
    ('weread', 'https://rescdn.qqmail.com/node/wr/wrpage/style/images/independent/favicon/favicon_32h.png',
    'https://weread.qq.com/'),
    ('163', 'https://s1.music.126.net/style/favicon.ico?v20180823',
    'https://music.163.com/?from=itab'),
    ('lindo', 'https://linux.do/uploads/default/optimized/3X/9/d/9dd49731091ce8656e94433a26a3ef36062b3994_2_32x32.png',
    'https://linux.do/'),
    ('huoshan', 'https://p3-scmimg.bytescm.com/tos-cn-i-rn3s1tazwm/lab/mt-portal/mt-portal-fe/static/image/favicon.8a2e2043.ico~tplv-rn3s1tazwm-resize:0:0:q80.image',
    'https://translate.volcengine.com/'),
    ('ed', 'https://easy-dotnet.com/img/favicon.ico',
    'https://easy-dotnet.com/'),
    ('vela', 'https://kubevela.io/zh/img/favicons/favicon.ico',
    'https://kubevela.io/zh/')
    ''');
  }
}
